package org.goyo.main.services.system.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.goyo.common.exception.DBException;
import org.goyo.common.exception.ServiceException;
import org.goyo.common.utils.ToolUtils;
import org.goyo.main.domain.dao.system.ISysMenuDao;
import org.goyo.main.domain.dao.system.ISysUserMenuDao;
import org.goyo.main.domain.entity.system.SysMenu;
import org.goyo.main.domain.entity.system.SysUserMenu;
import org.goyo.main.services.system.ISysMenuService;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class SysMenuServiceImpl implements ISysMenuService{
	
	@Resource
	private ISysMenuDao sysMenuDao;
	@Resource
	private ISysUserMenuDao sysUserMenuDao;
	
	@Override
	public List<SysMenu> getAllSysMenu() throws DBException,
			ServiceException{
		List<SysMenu> list = sysMenuDao.findByCond(null, null);
		return list;
	}
	
	

	@Override
	public String saveOrUpdateInfo(SysMenu info) throws DBException,ServiceException {
		String uuid = null;
		if(info.getUuid() == null || "".equals(info.getUuid())){
			info.setUuid(null);
			uuid = sysMenuDao.save(info);
		}else{
			sysMenuDao.update(info);
			uuid = info.getUuid();
		}
		return uuid;
	}

	@Override
	public void delByUuids(String[] uuids) throws DBException, ServiceException {
		sysMenuDao.delBatch(SysMenu.class, "uuid", uuids);
	}
	
	/**
	 * 查找菜单
	 */
	@Override
	public List<SysMenu> findByUserId(String userId) throws DBException,
			ServiceException {
		//查找菜单父级菜单
		List<SysMenu> level1List = sysUserMenuDao.findByUserIdAndLevel(userId, 1);
		List<SysMenu> level2List = sysUserMenuDao.findByUserIdAndLevel(userId, 2);
		for(SysMenu level1 :level1List){
			List<SysMenu> menus = new ArrayList<SysMenu>();
			for (SysMenu level2 : level2List) {
				if(level1.getUuid().equals(level2.getParentId()))
					menus.add(level2);
			}
			level1.setMenus(menus);
		}
//		
//		//获取用户菜单
//		List<SysUserMenu> userMenus = sysUserMenuDao.findByUserId(userId);
//		//查找菜单父级菜单
//		Set<String > menuIdSet = ToolUtils.getFiles(userMenus, "menuId");
//		//获取菜单表
//		List<SysMenu> menu1  = sysMenuDao.findBylevel(1);
//		List<SysMenu> menu2 = null;
//		if(menuIdSet != null){
//			menu2 = sysMenuDao.findByParams(SysMenu.class, "uuid", menuIdSet.toArray());
//			for(SysMenu sysMenu : menu1){
//				List<SysMenu> menus = new ArrayList<SysMenu>();
//				for(SysMenu menu : menu2 ){
//					if(sysMenu.getUuid().equals(menu.getParentId()))
//						menus.add(menu);
//				}
//				sysMenu.setMenus(menus);
//			}
//		}
		
		return level1List;
	}

	@Override
	public List<SysMenu> findBySupper() throws DBException, ServiceException {
		//查找菜单父级菜单
		List<SysMenu> level1List = sysUserMenuDao.findByLevel(1);
		List<SysMenu> level2List = sysUserMenuDao.findByLevel(2);
		for(SysMenu level1 :level1List){
			List<SysMenu> menus = new ArrayList<SysMenu>();
			for (SysMenu level2 : level2List) {
				if(level1.getUuid().equals(level2.getParentId()))
					menus.add(level2);
			}
			level1.setMenus(menus);
		}
		return level1List;
	}
}
